package com.tgy.leetcode;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @Author: tgy
 * @Date: 12/30/20 9:14 AM
 */
public class _1046_最后一块石头的重量 {

    public int lastStoneWeight(int[] stones) {

        if (stones == null || stones.length == 0) {
            return 0;
        }

        if (stones.length == 1) {

            return stones[0];
        }


        Queue<Integer> queue = new PriorityQueue<>((o1, o2) -> {

            return o2 - o1;
        });

        for (int stone : stones) {

            queue.add(stone);
        }

        while (queue.size() >= 2) {

            int big = queue.poll();
            int small = queue.poll();

            if (small != big) {

                queue.add(big - small);
            }
        }


        return queue.isEmpty()?0:queue.poll();
    }
}
